<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="字典名称" prop="selDicionariesKey">
        <el-select v-model="dataForm.selDicionariesKey" filterable placeholder="数据字典" style="width:100%" @change="dicionariesKeyChage">
          <el-option v-for="item in dicionariesKeyList" :key="item.name" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典码" prop="code">
        <el-input v-model="dataForm.code" placeholder="字典码"></el-input>
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input v-model="dataForm.value" placeholder="字典值"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序值"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dicionariesKey: {
            name: '',
            type: ''
            
          },
          code: '',
          value: '',
          remark: '',
          sort: '',
          selDicionariesKey: ''
        },
        dicionariesKeyList: [],
        dataRule: {
          selDicionariesKey: [{
            required: true,
            message: '请选择字典类型',
            trigger: 'change'
          }],
          code: [{
            required: true,
            message: '字典码不能为空',
            trigger: 'blur'
          }],
          value: [{
            required: true,
            message: '字典值不能为空',
            trigger: 'blur'
          }],
          sort: [{
            required: true,
            message: '排序值不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id, dicionariesKeyList) {
        this.dicionariesKeyList = dicionariesKeyList
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdictionaries/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.sysdictionaries.code
                this.dataForm.value = data.sysdictionaries.value
                this.dataForm.remark = data.sysdictionaries.remark
                this.dataForm.sort = data.sysdictionaries.sort
                this.dataForm.dicionariesKey.name = data.sysdictionaries.name
                this.dataForm.dicionariesKey.type = data.sysdictionaries.type
                this.dataForm.selDicionariesKey = data.sysdictionaries.name
              }
            })
          }
        })
      },
      dicionariesKeyChage(selValue) {
        // this.dataForm.dicionariesKey.name=selName
        for (let item of this.dicionariesKeyList) {
          if (selValue === item.name) {
            this.dataForm.dicionariesKey.type = item.type
            this.dataForm.dicionariesKey.name = item.name
            break
          }
        }

      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdictionaries/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.dicionariesKey.name,
                'type': this.dataForm.dicionariesKey.type,
                'code': this.dataForm.code,
                'value': this.dataForm.value,
                'remark': this.dataForm.remark,
                'sort': this.dataForm.sort
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }

</script>
